package com.bicon.botu.mall.base.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.mall.base.dto.CarefullyChosenAppDto;
import com.bicon.botu.mall.base.dto.CarefullyChosenDto;
import com.bicon.botu.mall.base.entity.CarefullyChosen;
import com.bicon.botu.mall.base.entity.Channel;
import com.bicon.botu.mall.base.mapper.CarefullyChosenMapper;
import com.bicon.botu.mall.base.service.ICarefullyChosenService;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import com.bicon.botu.core.base.common.utils.JsonUtil;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 为你精选 服务实现类
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@Service
@Transactional
public class CarefullyChosenServiceImpl extends ServiceImpl<CarefullyChosenMapper, CarefullyChosen> implements ICarefullyChosenService {
    @Autowired
    RedisHandler redisHandler;
    private static final String REDIS_KEY = MallHomePageConstants.BOTU + MallHomePageConstants.REDIS_KEY_DELIMITER + MallHomePageConstants.BASE +
            MallHomePageConstants.REDIS_KEY_DELIMITER + MallHomePageConstants.CAREFULLY_CHOSEN;

    /**
     * 批量新增为你精选图
     *
     * @param carefullyChosenList 精选图对象集合
     * @return
     */
    @Override
    public boolean insert(List<CarefullyChosen> carefullyChosenList) {
        boolean result;
        Integer rows = new CarefullyChosen().selectCount(new EntityWrapper<CarefullyChosen>().eq("is_big", MallHomePageConstants.IS_SMALL)
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        for (CarefullyChosen carefullyChosen : carefullyChosenList) {
            if (Objects.equals(carefullyChosen.getIsBig(), MallHomePageConstants.IS_SMALL)) {
                //设置序号为最大值
                carefullyChosen.setSort(++rows);
                carefullyChosen.setIsBig(MallHomePageConstants.IS_SMALL);
            } else {
                carefullyChosen.setSort(0);
                carefullyChosen.setIsBig(MallHomePageConstants.IS_BIG);
            }
            carefullyChosen.preInsert();
            result = carefullyChosen.insert();
            if (!result) {
                return false;
            }
        }
        //初始化
        this.initCarefullyChosenToRedis();
        return true;
    }

    /**
     * 删除为你精选图
     *
     * @param idJson 为你精选Id集合
     * @return
     */
    @Override
    public BaseResult delete(String idJson) {
        ArrayList<String> idList = JsonUtil.fromJson(idJson, ArrayList.class);
        boolean result;
        if (idList != null && idList.size() > 0) {
            for (int i = 0; i < idList.size(); i++) {
                CarefullyChosen carefullyChosen = new CarefullyChosen().selectOne(new EntityWrapper<CarefullyChosen>().eq("id", idList.get(i))
                        .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
                if (null == carefullyChosen) {
                    return new BaseResult(MallHomePageConstants.RESPONSE_CODE_404, MallHomePageConstants.RESPONSE_MESSAGE_404);
                }
                result = carefullyChosen.deleteById();
                if (!result) {
                    return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
                }
            }
        }
        //修改后重新初始化
        this.initCarefullyChosenToRedis();
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改为你精选标题
     *
     * @param title
     * @return
     */
    @Override
    public BaseResult updateTitle(String title) {
        boolean result;
        //获取所有为你精选图
        List<CarefullyChosen> carefullyChosenList = new CarefullyChosen().selectList(new EntityWrapper<CarefullyChosen>()
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        if (carefullyChosenList.size() != 0) {
            for (CarefullyChosen carefullyChosen : carefullyChosenList) {
                carefullyChosen.setTitle(title);
                result = carefullyChosen.updateById();
                if (!result) {
                    return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
                }
            }
        }
        //修改频道列表的标题
        Channel channel = new Channel().selectOne(new EntityWrapper<>().eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL).eq("channel_type", MallHomePageConstants.CAREFULLY_CHOSEN_TYPE));
        channel.setChannelTitle(title);
        result = channel.updateById();
        if (!result) {
            return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
        }
        //修改后重新初始化
        this.initCarefullyChosenToRedis();
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改为你精选图
     *
     * @param carefullyChosenList 精选图对象集合
     * @return
     */
    @Override
    public BaseResult update(List<CarefullyChosen> carefullyChosenList) {
        boolean result;
        for (CarefullyChosen carefullyChosen : carefullyChosenList) {
            //根据Id判断是否存在
            CarefullyChosen carefullyChosen1 = new CarefullyChosen().selectOne(new EntityWrapper<CarefullyChosen>().eq("id", carefullyChosen.getId()));
            if (null == carefullyChosen1) {
                return new BaseResult(MallHomePageConstants.RESPONSE_CODE_404, MallHomePageConstants.RESPONSE_MESSAGE_404);
            }
            //修改为你精选表
            result = carefullyChosen.updateById();
            if (!result) {
                return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
            }
        }
        //修改后重新初始化
        this.initCarefullyChosenToRedis();
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }

    /**
     * 初始化所有为你精选图到redis
     *
     * @return
     */
    @Override
    public BaseResult initCarefullyChosenToRedis() {
        //清除缓存
        redisHandler.getLocalRedisTemplate().remove(REDIS_KEY, "");
        //查询出所有正常的为你精选图
        List<CarefullyChosen> carefullyChosenList = new CarefullyChosen().selectList(new EntityWrapper<CarefullyChosen>()
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL)
                .eq("flag", MallHomePageConstants.ON).orderBy("sort", true));
        Channel channel = new Channel().selectOne(new EntityWrapper<Channel>().eq("channel_type", MallHomePageConstants.CAREFULLY_CHOSEN_TYPE)
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        List<CarefullyChosenAppDto> carefullyChosenAppDtos = new ArrayList<>();
        for (CarefullyChosen carefullyChosen : carefullyChosenList) {
            CarefullyChosenAppDto carefullyChosenAppDto = new CarefullyChosenAppDto();
            BeanMapper.copy(carefullyChosen, carefullyChosenAppDto);
            carefullyChosenAppDtos.add(carefullyChosenAppDto);
        }
        CarefullyChosenDto carefullyChosenDto = new CarefullyChosenDto();
        carefullyChosenDto.setChannelTitle(channel.getChannelTitle());
        carefullyChosenDto.setChannelType(channel.getChannelType());
        carefullyChosenDto.setCarefullyChosenList(carefullyChosenAppDtos);
        redisHandler.getLocalRedisTemplate().cacheHash(REDIS_KEY, "", channel.getChannelType() + "", JsonUtil.toJson(carefullyChosenDto));
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }

    /**
     * 获取为你精选频道详情
     *
     * @return
     */
    @Override
    public CarefullyChosenDto getCarefullyChosenDetailFromRedis() {
        //从redis上获取为你精选图详情
        Map<Object, Object> map = redisHandler.getLocalRedisTemplate().getHashMap(REDIS_KEY, "");
        String carefullyChosenDtoJson = (String) map.get(String.valueOf(MallHomePageConstants.CAREFULLY_CHOSEN_TYPE));
        CarefullyChosenDto carefullyChosenDto = JSONObject.parseObject(carefullyChosenDtoJson, CarefullyChosenDto.class);
        return carefullyChosenDto;
    }

    /**
     * 修改排序
     *
     * @param carefullyChosenList 精选图对象集合
     * @return
     */
    @Override
    public BaseResult updaterSort(List<CarefullyChosen> carefullyChosenList) {
        boolean result;
        Integer rows = new CarefullyChosen().selectCount(new EntityWrapper<CarefullyChosen>().eq("is_big", MallHomePageConstants.IS_SMALL)
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL));
        for (CarefullyChosen carefullyChosen : carefullyChosenList) {
            //是小图则删除所有小图后新增
            if (Objects.equals(carefullyChosen.getIsBig(), MallHomePageConstants.IS_SMALL)) {
                result = new CarefullyChosen().delete(new EntityWrapper<CarefullyChosen>().eq("id", carefullyChosen.getId()));
                if (!result) {
                    return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
                }
                //新增
                carefullyChosen.setSort(++rows);
                carefullyChosen.preInsert();
                result = carefullyChosen.insert();
                if (!result) {
                    return new BaseResult(MallHomePageConstants.RESPONSE_CODE_500, MallHomePageConstants.RESPONSE_MESSAGE_500);
                }
            }
        }
        this.initCarefullyChosenToRedis();
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }
}